package com.wavehi.web;

import com.wavehi.pojo.PageForNav;
import com.wavehi.pojo.User;
import com.wavehi.service.IUserService;
import com.wavehi.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author WaveHi
 * @Date 25/9/2021 上午 11:21
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description
 */
@WebServlet("/userServlet")
public class UserServlet extends BaseServlet{
    //获取业务层对象
    private IUserService ius = new UserServiceImpl();

    /**
     * 无实际意义，仅供开发使用
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void base(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取需要要删除用户对应的id
        String id = req.getParameter("id");
        //删除用户
        ius.deleteUser(Long.parseLong(id));
        //转发
        page(req, resp);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取修改后的数据
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //封装用户信息
        User user = new User(Long.parseLong(id), username, password);
        //对用户信息进行修改
        ius.updateUser(user);
        //转发
        page(req, resp);
    }

    /**
     * 获取用户信息并转发到更新界面
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户对应的id值
        String id = req.getParameter("id");
        //获取用户信息
        User user = ius.getUser(Long.parseLong(id));
        //将用户信息绑定到请求域
        req.setAttribute("user", user);
        //获取请求转发对象
        RequestDispatcher dispatcher = req.getRequestDispatcher("/page/root/user_edit.jsp");
        //转发
        dispatcher.forward(req, resp);
    }

    /**
     * 过滤查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void filter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取筛选条件数据
        String username = req.getParameter("username");
        String id = req.getParameter("id");
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        //检验数据
        pageSize = pageSize != null && !"".equals(pageSize.trim()) ?
                pageSize.trim() : PageForNav.PAGE_SIZE.toString();
        pageNo = pageNo != null && !"".equals(pageNo.trim()) ? pageNo.trim() : "1";
        int pageNoInt = Integer.parseInt(pageNo);
        username = username != null ? username.trim() : null;
        Long idLong = id != null && !"".equals(id.trim()) ? Long.parseLong(id.trim()) : null;
        //封装页数据
        PageForNav<User> userPageForNav = new PageForNav<>();
        userPageForNav.setPageSize(Integer.parseInt(pageSize));
        //从数据库获取数据
        int dataTotalCount = ius.getPartOfUsers(new User(idLong, username, null)).size();
        int pageTotalCount = dataTotalCount % userPageForNav.getPageSize() == 0 ?
                (dataTotalCount / userPageForNav.getPageSize()) : (dataTotalCount /userPageForNav.getPageSize() + 1);
        pageNoInt = pageNoInt > pageTotalCount ? pageTotalCount : pageNoInt;
        List<User> datas = ius.getPartOfUsers(new User(idLong,
                username, null), pageNoInt, userPageForNav.getPageSize());
        //设置页数据
        userPageForNav.setDatas(datas);
        userPageForNav.setDataTotalCount(dataTotalCount);
        userPageForNav.setPageNo(pageNoInt);
        userPageForNav.setPageTotalCount(pageTotalCount);
        userPageForNav.setUrl("userServlet?action=filter&id=" + id + "&username=" + username);
        //绑定页数据和参数数据到会话域
        req.setAttribute("page", userPageForNav);
        req.setAttribute("username", username);
        req.setAttribute("id", id);
        //获取请求转发对象
        RequestDispatcher dispatcher = req.getRequestDispatcher("/page/root/user_display.jsp");
        //转发
        dispatcher.forward(req, resp);
    }

    /**
     * 分页显示用户数据
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页数
        String pageNo = req.getParameter("pageNo");
        //获取显示信息条数
        String pageSize = req.getParameter("pageSize");
        pageSize = pageSize != null && !"".equals(pageSize.trim()) ?
                pageSize.trim() : PageForNav.PAGE_SIZE.toString();
        Integer pageSizeInt = Integer.valueOf(pageSize);
        //检验数据
        PageForNav<User> pageForNav = new PageForNav();
        pageForNav.setPageSize(Integer.valueOf(pageSize));
        pageNo = pageNo != null && !"".equals(pageNo.trim()) ? pageNo.trim() : "1";
        //获取用户数据
        int dataTotalCount = ius.getAllUsers().size();
        int pageTotalCount = dataTotalCount % pageSizeInt == 0 ?
                (dataTotalCount / pageSizeInt) : (dataTotalCount / pageSizeInt + 1);
        int pageNoInt = Integer.parseInt(pageNo);
        pageNoInt = pageNoInt > pageTotalCount ? pageTotalCount : pageNoInt;
        //设置页数据
        pageForNav.setDataTotalCount(dataTotalCount);
        pageForNav.setPageTotalCount(pageTotalCount);
        pageForNav.setPageNo(pageNoInt);
        pageForNav.setUrl("userServlet?action=page");
        pageForNav.setDatas(ius.getPartOfUsers(pageNoInt, pageForNav.getPageSize()));
        //绑定页数据到请求域
        req.setAttribute("page", pageForNav);
        //获取转发对象
        RequestDispatcher dispatcher = req.getRequestDispatcher("/page/root/user_display.jsp");
        //转发
        dispatcher.forward(req, resp);
    }
}
